﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GpCommon.Helpers
{
    public class CommonUtility
    {
        public static double RoundUp(double value, int decimalPlaces)
        {
            double factor = Math.Pow(10, decimalPlaces);
            return Math.Ceiling(value * factor) / factor;
        }
        public static float ToFloat(string value)
        {
            return float.TryParse(value, out float temp) ? temp : 0.0f;
        }
        public static int ToInt32(string value)
        {
            return int.TryParse(value, out int temp) ? temp : 0;
        }


        public static bool ExportGPToImg(List<string> codes, string path)
        {
            try
            {
                // 创建一个 Bitmap 对象，表示画布
                Bitmap bitmap = new Bitmap(1000, 1000);

                // 创建一个 Graphics 对象，用于在 Bitmap 上绘图
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    // 设置画布背景色
                    graphics.Clear(System.Drawing.Color.White);

                    // 设置绘图字体和颜色
                    Font font = new Font("Arial", 18);
                    SolidBrush brush = new SolidBrush(System.Drawing.Color.Black);

                    float y = 10f; // 纵坐标

                    float x = 10f;
                    // 在画布上逐个绘制成语
                    foreach (string code in codes)
                    {
                        graphics.DrawString(code, font, brush, new PointF(x, y));
                        x += 200;
                        if (x > 1000)
                        {
                            x = 10;
                            y += 40;
                        }
                    }
                }

                // 保存绘制好的图像为文件
                bitmap.Save(path, ImageFormat.Png);

                // 释放资源
                bitmap.Dispose();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static bool IsValidCode(string code, string name)
        {
            if (name.Contains("ST")
            || (!code.StartsWith("60")
            && !code.StartsWith("00")))
            {
                return false;
            }
            return true;
        }
    }
}
